package com.monas.server.parser;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.LinkedList;

public abstract class MonasParser {

	private MonasHeader[] headers = null;

	private LinkedList<String> lines = null;

	public MonasParser(MonasHeader... headers) {
		this.headers = headers;
		lines = new LinkedList<String>();
	}

	public abstract boolean isNextBegin(String line);

	public abstract boolean isFinish(String line);
	
	public abstract String getTableName();

	public abstract LinkedList<Object[]> getRecords();

	public void readFile(String fileName) throws Exception {

		lines.clear();

		BufferedReader reader = new BufferedReader(new FileReader(fileName));

		try {
			String line = "";
			boolean save = false;

			while ((line = reader.readLine()) != null) {
				if (save && isFinish(line)) {
					break;
				}

				if (isNextBegin(line)) {
					save = true;
					continue;
				}

				if (save) {
					lines.add(line);
				}
			}
		} finally {
			reader.close();
		}
	}

	public MonasHeader[] getHeaders() {
		return headers;
	}

	public LinkedList<String> getLines() {
		return lines;
	}

}
